// pages/actives/index.js

import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      { id: 0, value: "活动", isActive: true },
      { id: 1, value: "活动管理", isActive: false },
    ],
    activesLists: [],
    collectedLists: [],
    activeId: 0,
    userId: 1,
    isCollect: false,
  },

  onShow: function () {
    this.getActiveList();
    this.getMyCollect();
  },

  //编辑点击
  activeEdit(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/activesEdit/index?id=" + id + "",
    });
  },

  //收藏点击事件
  async handleCollect(e) {
    //获取用户收藏数据
    // const res = await request({ url: "/user/myFavorites?wx_user_id=1" });
    // let collectArr = res.data.data;

    let id = e.currentTarget.dataset.id;
    let collectArr = this.data.collectedLists;

    let isCollect = collectArr.findIndex((v) => v.active_id === id);

    if (isCollect == -1) {
      // 判断产品是否存在收藏列表中，如果存在则取消收藏
      this.Collect(id);
      this.getActiveList();
      this.getMyCollect();
    } else {
      // 判断产品是否存在收藏列表中，如果不存在则添加收藏
      this.DelCollect(id);
      this.getActiveList();
      this.getMyCollect();
    }
  },

  //加入收藏
  async Collect(params) {
    //campus_activites_id
    let id = params;
    let userId = 1;
    const res = await request({
      url:
        "/activites/addFavorites?campus_activites_id=" +
        id +
        "&wx_user_id=" +
        userId +
        "",
    });
    // wx.showToast({
    //   title: "收藏成功",
    //   icon: "success",
    //   duration: 2000,
    // });
    // this.getActiveList();
    // this.getMyCollect();
  },

  //取消收藏
  async DelCollect(params) {
    let id = params;
    let userId = 1;
    const res = await request({
      url:
        "/activites/unFavorite?campus_activites_id=" +
        id +
        "&wx_user_id=" +
        userId +
        "",
    });
    // wx.showToast({
    //   title: "取消收藏成功",
    //   icon: "cancel",
    //   duration: 2000,
    // });
    // this.getActiveList();
    // this.getMyCollect();
  },

  //活动删除
  activesDel(e) {
    //console.log(999, e.currentTarget.dataset.id);
    let id = e.currentTarget.dataset.id;
    let that = this;
    wx.showModal({
      title: "提示",
      content: "是否删除此活动？",
      success(res) {
        if (res.confirm) {
          // //console.log("用户点击确定");
          request({
            url: "/activites/deleteCampusActivites?active_id=" + id + "",
          });
          that.getActiveList();
          that.getMyCollect();
        } else if (res.cancel) {
          // //console.log("用户点击取消");
          wx.showToast({
            title: "已取消",
            icon: "cancel",
            duration: 2000,
          });
        }
      },
    });

    // const res = await request({
    //   url: "/activites/deleteCampusActivites?active_id=" + id + "",
    // });
    // this.getActiveList();
    // this.getMyCollect();
  },

  //获取活动信息
  async getActiveList() {
    const res = await request({ url: "/activites/findAllCampusActivites" });
    ////console.log("活动数据", res);
    //给数据添加isSelect属性
    let activesListsArr = res.data.data;
    activesListsArr.forEach((v) => {
      v.isSelect = false;
    });

    //获取收藏数据，判断是否被收藏，收藏之后会显示收藏星标
    const collectRes = await request({ url: "/user/myFavorites?wx_user_id=1" });
    let collectResArr = [];
    collectResArr = collectRes.data.data;
    //console.log(223333, collectResArr);

    // let index = collectResArr.findIndex(
    //   (v) => v.active_id === activesListsArr.active_id
    // );

    //判断未被收藏，未收藏之后会显示收藏星标

    for (let i = 0; i < collectResArr.length; i++) {
      for (let j = 0; j < activesListsArr.length; j++) {
        if (collectResArr[i].active_id == activesListsArr[j].active_id) {
          activesListsArr[j].isSelect = true;
        } else {
        }
      }
    }

    this.setData({
      activesLists: res.data.data,
    });
  },

  //获取我的收藏数据
  async getMyCollect() {
    const res = await request({ url: "/user/myFavorites?wx_user_id=1" });
    //this.setData({ foundList: res.data.data });
    ////console.log("res", res);
    this.setData({
      collectedLists: res.data.data,
    });
  },

  //报名点击事件
  async applyFn(e) {
    const { id } = e.currentTarget.dataset;
    // //console.log(222, id);
    const res = await request({ url: "/activites/findAllCampusActivites" });
    // //console.log("活动数据", res);
  },

  //标题点击事件
  handleTabsItemChange(e) {
    ////console.log(e.detail);
    //获取传递过来的数据
    const { index } = e.detail;
    //获取原数组，修改源数组

    let { tabs } = this.data;
    //遍历tabs,如果index等于数组中的index,则修改数组中的isActive
    tabs.forEach((v, i) =>
      i === index ? (v.isActive = true) : (v.isActive = false)
    );
    this.setData({
      tabs,
    });
  },
});
